package org.krishna.service;

import java.util.ArrayList;
import java.util.List;

import org.krishna.Dao.SaveData;
import org.krishna.Dto.BuildDTOUtils;
import org.krishna.Dto.User;
import org.krishna.json.CreateDetails;
import org.krishna.model.Category;
import org.krishna.model.UserDetails;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("serviceHandler")
@Transactional(propagation = Propagation.REQUIRED)
public class ServiceHandlerImpl implements ServiceHandler {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(ServiceHandlerImpl.class);

	@Autowired
	@Qualifier("saveData")
	SaveData saveData;

	@Override
	@Transactional
	public boolean registerUser(CreateDetails details) {
		boolean result = false;
		UserDetails userDetails = saveData.getUserByUserName(details
				.getUserName());
		if (userDetails == null) {
			LOGGER.info("No user exists with the userName "
					+ details.getUserName());
			userDetails.setFirstName(details.getFirstName());
			userDetails.setLastName(details.getLastName());
			userDetails.setRollNo(details.getRollNo());
			userDetails.setUserName(details.getUserName());
			userDetails.setPassword(details.getPassword());
			userDetails.setMailId(details.getMailId());
			userDetails.setContactNo(Integer.parseInt(details.getContactNo()));
			Category category = saveData.getCategoryByName(details
					.getCategory());
			if (null != category) {
				LOGGER.info("Category Object retrieved by Name from database");
			} else {
				LOGGER.error("Category of the user is null");
			}
			userDetails.setCategory(category);
			userDetails.setMaxLimit(5);
			userDetails.setBooksTaken(0);
			LOGGER.info("User Object created for saving to DB");

			result = saveData.saveUserDetailsData(userDetails);
		} else {
			LOGGER.error("User with user name " + details.getUserName()
					+ " already exists in the application");
			LOGGER.error("Please try again with another userName");
		}
		return result;

	}

	
	@Override
	@Transactional
	public List<User> getAllStudents() {
		List<User> users = new ArrayList<User>();
		List<UserDetails> userDetails = saveData.getAllStudents();
		if (userDetails != null) {
			LOGGER.info("No of Students are : "+userDetails.size());
			for (UserDetails userDetail : userDetails) {
				users.add(BuildDTOUtils.buildUserDTo(userDetail));
			}
		}
		return users;
	}

	@Override
	@Transactional
	public User getUserByUserName(String userName) {
		User user = null;
		UserDetails userDetails = saveData.getUserByUserName(userName);
		if (userDetails != null) {
			user = BuildDTOUtils.buildUserDTo(userDetails);
		}
		return user;
	}


	@Override
	public boolean updateUser(String userName, String mobileNo, String emailId) {
		boolean result = false;
		UserDetails userDetails = saveData.getUserByUserName(userName);
		if (userDetails != null) {
			LOGGER.info("user is available in the DB");
			userDetails.setContactNo(Integer.parseInt(mobileNo));
			userDetails.setMailId(emailId);
			result = saveData.updateUserDetailsData(userDetails);
		}else{
			LOGGER.info("No user is available in the DB with the username "+userName);
		}
		return result;
	}


	
}
